package com.example.demo.juc;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolExecutorTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        RejectedExecutionHandler handler = new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println("任务被拒绝了");
            }
        };

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                20,
                1000,
                java.util.concurrent.TimeUnit.SECONDS,
                new java.util.concurrent.LinkedBlockingQueue<>(1000),
                handler
        );

        /**
         * submit任务，等待线程池execute执行FutureTask类的get方法时，会把主线程封装成WaitNode节点并保存在waiters链表中， 并阻塞等待运行结果；FutureTask任务执行完成后，通过UNSAFE设置waiters相应的waitNode为null，并通过LockSupport类unpark方法唤醒主线程；
         * ------
         * 著作权归@pdai所有
         * 原文链接：https://pdai.tech/md/java/thread/java-thread-x-juc-executor-ThreadPoolExecutor.html
         */
        Future<String> future = executor.submit(() -> {
            System.out.println("任务执行了");
            return "hello";
        });
        future.get();


        executor.getPoolSize();
        executor.getCorePoolSize();
        executor.getActiveCount();
        executor.getCompletedTaskCount();
        executor.getTaskCount();
        executor.isShutdown();
        executor.isTerminated();
    }


}
